package com.example.healkep;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.healkep.MainActivity.MainActivity;
import com.example.healkep.bean.bg.DateBean;
import com.example.healkep.datepicker.DateFormatUtils;
import com.example.healkep.listener.bg.CalendarViewAdapter;
import com.example.healkep.listener.bg.OnPagerChangeListener;
import com.example.healkep.listener.bg.OnSingleChooseListener;
import com.example.healkep.utils.bg.CalendarUtil;
import com.example.healkep.weiget.bg.CalendarView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainBg extends AppCompatActivity {
    //test
    private CalendarView calendarView;
    private TextView chooseDate;
    private int bg_year,bg_month,bg_day;

    private int[] cDate = CalendarUtil.getCurrentDate();

    //单日记录的六个时段血糖值
    private double[] day_bg = new double[10];  //记录当天10个时间点血糖值

    private double[] bb_week = new double[7]; //七日记录的空腹血糖值
    private double[] ab1_week = new double[7];  //七日记录的早餐后1h血糖值
    private double[] ab2_week = new double[7];  //七日记录的早餐后2h血糖值
    private double[] bl_week = new double[7];   //七日记录的午餐前血糖值
    private double[] al1_week = new double[7];   //七日记录的午餐后1h血糖值
    private double[] al2_week = new double[7];   //七日记录的午餐后2h血糖值
    private double[] bd_week = new double[7];   //七日记录的晚餐前血糖值
    private double[] ad1_week = new double[7];   //七日记录的晚餐后1h血糖值
    private double[] ad2_week = new double[7];   //七日记录的晚餐后2h血糖值
    private double[] bs_week = new double[7];   //七日记录的睡前血糖值

    private double[] bb_month = new double[30]; //三十日记录的空腹血糖值
    private double[] ab1_month = new double[30];  //三十日记录的早餐后1h血糖值
    private double[] ab2_month = new double[30];  //三十日记录的早餐后2h血糖值
    private double[] bl_month = new double[30];   //三十日记录的午餐前血糖值
    private double[] al1_month = new double[30];   //三十日记录的午餐后1h血糖值
    private double[] al2_month = new double[30];  //三十日记录的早餐后2h血糖值
    private double[] bd_month = new double[30];   //三十日记录的晚餐前血糖值
    private double[] ad1_month = new double[30];   //三十日记录的晚餐1h后血糖值
    private double[] ad2_month = new double[30];  //三十日记录的早餐后2h血糖值
    private double[] bs_month = new double[30];   //三十日记录的睡前后血糖值

    private double[] weight = new double[43];   //孕周体重（42周，weight[0]存放孕妇初始体重用于推荐图，下标从1开始为了使孕周与下标匹配）

    ArrayList<String> time_week = new ArrayList<>();  //记录作为横坐标轴的七天日期值（由用户选择的日期根据timestamp回推七天所得，不包括当天）
    ArrayList <String> time_month = new ArrayList<>();  //记录作为横坐标轴的三十天日期值（由用户选择的日期根据timestamp回推七天所得，不包括当天）

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        final Intent intent = getIntent();
        int id = intent.getIntExtra("id", -1);
        int preg_num = intent.getIntExtra("preg_num", 1);  //默认单胎
        double weight_ori = intent.getDoubleExtra("weight_ori", 0);  //初始化信息时的初始体重
        double BMI = intent.getDoubleExtra("BMI",20);  //BMI值
        int week_ori = intent.getIntExtra("week_ori", 1);  //初始化信息时的孕周

        //输入时刻的孕周
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String myDate = format.format(new Date());
        int preg_week = (int)com.example.healkep.utils.GetWeek.week(myDate);

        bg_year = cDate[0];
        bg_month = cDate[1];
        bg_day = cDate[2];


        super.onCreate(savedInstanceState);
        setContentView(R.layout.bg_main);
        final TextView title = (TextView) findViewById(R.id.title_time);
        //当前选中的日期
        //chooseDate = findViewById(R.id.choose_date);

        calendarView = (CalendarView) findViewById(R.id.calendar);

        calendarView
                .setStartEndDate("2016.1", "2028.12")
                .setDisableStartEndDate("2016.10.10", "2028.10.10")
                .setInitDate(cDate[0] + "." + cDate[1])
                .setSingleDate(cDate[0] + "." + cDate[1] + "." + cDate[2])
                .setOnCalendarViewAdapter(R.layout.item_layout, new CalendarViewAdapter() {
                    @Override
                    public TextView[] convertView(View view, DateBean date) {
                        TextView solarDay = view.findViewById(R.id.solar_day);
                        TextView lunarDay = view.findViewById(R.id.lunar_day);
                        return new TextView[]{solarDay, lunarDay};
                    }
                })
                .init();


        title.setText(cDate[0] + "年" + cDate[1] + "月");
        //chooseDate.setText("当前选中的日期：" + cDate[0] + "年" + cDate[1] + "月" + cDate[2] + "日");

        calendarView.setOnPagerChangeListener(new OnPagerChangeListener() {
            @Override
            public void onPagerChanged(int[] date) {
                title.setText(date[0] + "年" + date[1] + "月");
            }
        });

        String year_bg, month_bg, day_bg2;
        year_bg = Integer.toString(bg_year);
        if (bg_month < 10) {
            month_bg = "0" + Integer.toString(bg_month);
        } else {
            month_bg = Integer.toString(bg_month);
        }
        if (bg_day < 10) {
            day_bg2 = "0" + Integer.toString(bg_day);
        } else {
            day_bg2 = Integer.toString(bg_day);
        }

        String timeStamp = (year_bg + "-" + month_bg + "-" + day_bg2).toString();
        System.out.println(timeStamp);
        Long timeNow = DateFormatUtils.str2Long(timeStamp, false);
        time_week.clear();
        time_month.clear();
        for (int i = 0; i < 7; i++) {
            time_week.add(DateFormatUtils.long2Str(timeNow - (7 - i) * 24 * 60 * 60 * 1000L, false));
        }   //循环获取七天的数值下标，通过long2str的false方法将传递过来的精确timestamp类型日期的时分秒去掉
        for (int i = 0; i < 30; i++) {
            time_month.add(DateFormatUtils.long2Str(timeNow - (30 - i) * 24 * 60 * 60 * 1000L, false));
        }

        send(Integer.toString(id), timeStamp);

        calendarView.setOnSingleChooseListener(new OnSingleChooseListener() {
            @Override
            public void onSingleChoose(View view, DateBean date) {
                title.setText(date.getSolar()[0] + "年" + date.getSolar()[1] + "月");
                //if (date.getType() == 1) {
                //    chooseDate.setText("当前选中的日期：" + date.getSolar()[0] + "年" + date.getSolar()[1] + "月" + date.getSolar()[2] + "日");
                //}
                bg_year = date.getSolar()[0];
                bg_month = date.getSolar()[1];
                bg_day = date.getSolar()[2];
                Log.d("month", Integer.toString(bg_month));

                String year_bg, month_bg, day_bg2;
                year_bg = Integer.toString(bg_year);
                if (bg_month < 10) {
                    month_bg = "0" + Integer.toString(bg_month);
                } else {
                    month_bg = Integer.toString(bg_month);
                }
                if (bg_day < 10) {
                    day_bg2 = "0" + Integer.toString(bg_day);
                } else {
                    day_bg2 = Integer.toString(bg_day);
                }

                String timeStamp = (year_bg + "-" + month_bg + "-" + day_bg2).toString();
                System.out.println(timeStamp);
                Long timeNow = DateFormatUtils.str2Long(timeStamp, false);
                time_week.clear();
                time_month.clear();
                for (int i = 0; i < 7; i++) {
                    time_week.add(DateFormatUtils.long2Str(timeNow - (7 - i) * 24 * 60 * 60 * 1000L, false));
                }   //循环获取七天的数值下标，通过long2str的false方法将传递过来的精确timestamp类型日期的时分秒去掉
                for (int i = 0; i < 30; i++) {
                    time_month.add(DateFormatUtils.long2Str(timeNow - (30 - i) * 24 * 60 * 60 * 1000L, false));
                }

                send(Integer.toString(id), timeStamp);
            }
        });

        TextView input = findViewById(R.id.input);
        input.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainBg.this, ChooseInputBg.class);
                Bundle bd = new Bundle();
                bd.putInt("bg_month", bg_month);
                bd.putInt("bg_year", bg_year);
                bd.putInt("bg_day", bg_day);
                bd.putInt("id", id);
                intent.putExtras(bd);
                startActivity(intent);
            }
        });

        TextView viewGraph = findViewById(R.id.view);
        viewGraph.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainBg.this, ViewBgGraph.class);
                Bundle bun = new Bundle();
                bun.putDoubleArray("bg", day_bg);
                bun.putStringArrayList("time_week", time_week);
                bun.putStringArrayList("time_month", time_month);
                bun.putDoubleArray("bb_week", bb_week);
                bun.putDoubleArray("ab1_week", ab1_week);
                bun.putDoubleArray("ab2_week", ab2_week);
                bun.putDoubleArray("bl_week", bl_week);
                bun.putDoubleArray("al1_week", al1_week);
                bun.putDoubleArray("al2_week", al2_week);
                bun.putDoubleArray("bd_week", bd_week);
                bun.putDoubleArray("ad1_week", ad1_week);
                bun.putDoubleArray("ad2_week", ad2_week);
                bun.putDoubleArray("bs_week", bs_week);

                bun.putDoubleArray("bb_month", bb_month);
                bun.putDoubleArray("ab1_month", ab1_month);
                bun.putDoubleArray("ab2_month", ab2_month);
                bun.putDoubleArray("bl_month", bl_month);
                bun.putDoubleArray("al1_month", al1_month);
                bun.putDoubleArray("al2_month", al2_month);
                bun.putDoubleArray("bd_month", bd_month);
                bun.putDoubleArray("ad1_month", ad1_month);
                bun.putDoubleArray("ad2_month", ad2_month);
                bun.putDoubleArray("bs_month", bs_month);

                intent.putExtras(bun);
                startActivity(intent);
            }
        });

        TextView viewW = findViewById(R.id.vieww);
        viewW.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            MediaType JSON = MediaType.parse("application/json");
                            JSONObject json = new JSONObject();
                            System.out.println(id);
                            try {
                                json.put("id", id);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            OkHttpClient client = new OkHttpClient();  //创建http客户端
                            RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                            Request request = new Request.Builder()
                                    .url(LoginActivity.url+"/weight/view")
                                    .post(requestBody)
                                    .build();    //创造http请求
                            Response response = client.newCall(request).execute();  //执行发送的指令
                            String responseData = response.body().string();
                            JSONArray jsonArray = new JSONArray(responseData);
                            Arrays.fill(weight, 0);  //初始化0
                            weight[0] = weight_ori;
                            for (int i = 0; i < jsonArray.length(); i++) {
                                for(int j = 1; j < 43; j++) {
                                    if (jsonArray.getJSONObject(i).getInt("week") == j)
                                        weight[j] = jsonArray.getJSONObject(i).getDouble("weight");
                                }
                            }
                            System.out.println("weight: ");
                            System.out.println(weight[1]);

                            Intent intent = new Intent(MainBg.this, ViewWeight.class);
                            Bundle bun = new Bundle();
                            bun.putDoubleArray("weight", weight);
                            bun.putInt("id", id);
                            bun.putInt("preg_num", preg_num);
                            bun.putInt("week_ori", week_ori);
                            bun.putDouble("BMI", BMI);

                            intent.putExtras(bun);
                            startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainBg.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();
            }
        });

        TextView inputW = findViewById(R.id.inputw);
        inputW.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainBg.this, WeightInput.class);
                Bundle bun = new Bundle();
                bun.putInt("id", id);
                bun.putInt("preg_week", preg_week);

                intent.putExtras(bun);
                startActivity(intent);
            }
        });

        Button select1 = (Button) findViewById ( R.id.select1 );
        Button select2 = (Button) findViewById ( R.id.select2 );
        Button select4 = (Button) findViewById ( R.id.select4 );

        select1.setOnClickListener ( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(view.getId()==R.id.select1) {
                    // 添加点击响应事件
                    Intent intent1 = new Intent ( MainBg.this, MainActivity.class );
                    //启动
                    startActivity ( intent1 );
                }
            }
        } );
        //绑定监听事件
        select2.setOnClickListener ( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(view.getId()==R.id.select2) {
                    // 给bnt1添加点击响应事件
                    Intent intent1 = new Intent ( MainBg.this, FoodMainActivity.class );
                    //启动
                    startActivity ( intent1 );
                }
            }
        } );



        select4.setOnClickListener ( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(view.getId()==R.id.select4) {
                    // 添加点击响应事件
                    Intent intent1 = new Intent ( MainBg.this, MainUser.class );
                    //启动
                    startActivity ( intent1 );
                }
            }
        } );
    }


    public void send(String id, String timeStamp){

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    MediaType JSON = MediaType.parse("application/json");
                    JSONObject json = new JSONObject();
                    System.out.println(id);
                    try {
                        json.put("id", id);
                        json.put("time", timeStamp);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    OkHttpClient client = new OkHttpClient();  //创建http客户端
                    RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                    Request request = new Request.Builder()
                            .url(LoginActivity.url+"/bg/day")
                            .post(requestBody)
                            .build();    //创造http请求
                    Response response = client.newCall(request).execute();  //执行发送的指令
                    String responseData = response.body().string();
                    JSONArray jsonArray = new JSONArray(responseData);
                    Arrays.fill(day_bg, 0);  //初始化0
                    for (int i = 0; i < jsonArray.length(); i++) {
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 1)
                            day_bg[0] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 2)
                            day_bg[1] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 3)
                            day_bg[2] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 4)
                            day_bg[3] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 5)
                            day_bg[4] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 6)
                            day_bg[5] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 7)
                            day_bg[6] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 8)
                            day_bg[7] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 9)
                            day_bg[8] = jsonArray.getJSONObject(i).getDouble("bg_value");
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 10)
                            day_bg[9] = jsonArray.getJSONObject(i).getDouble("bg_value");
                    }

                    System.out.println("day_bg: ");
                    System.out.println(day_bg[0]);
                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainBg.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    MediaType JSON = MediaType.parse("application/json");
                    JSONObject json = new JSONObject();
                    try {
                        json.put("id", id);
                        json.put("time", timeStamp);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    OkHttpClient client = new OkHttpClient();  //创建http客户端
                    RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                    Request request = new Request.Builder()
                            .url(LoginActivity.url+"/bg/week")
                            .post(requestBody)
                            .build();    //创造http请求
                    Response response = client.newCall(request).execute();  //执行发送的指令
                    String responseData = response.body().string();
                    JSONArray jsonArray = new JSONArray(responseData);
                    Arrays.fill(bb_week, 0);
                    Arrays.fill(ab1_week, 0);
                    Arrays.fill(ab2_week, 0);
                    Arrays.fill(bl_week, 0);
                    Arrays.fill(al1_week, 0);
                    Arrays.fill(al2_week, 0);
                    Arrays.fill(bd_week, 0);
                    Arrays.fill(ad1_week, 0);
                    Arrays.fill(ad2_week, 0);
                    Arrays.fill(bs_week, 0);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 1) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    bb_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 2) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    ab1_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 3) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    ab2_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 4) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    bl_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 5) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    al1_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 6) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    al2_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 7) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    bd_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 8) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    ad1_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 9) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    ad2_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 10) {
                            for (int j = 0; j < 7; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_week.get(j)) == 0)
                                    bs_week[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainBg.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    MediaType JSON = MediaType.parse("application/json");
                    JSONObject json = new JSONObject();
                    try {
                        json.put("id", id);
                        json.put("time", timeStamp);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    OkHttpClient client = new OkHttpClient();  //创建http客户端
                    RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                    Request request = new Request.Builder()
                            .url(LoginActivity.url+"/bg/month")
                            .post(requestBody)
                            .build();    //创造http请求
                    Response response = client.newCall(request).execute();  //执行发送的指令
                    String responseData = response.body().string();
                    JSONArray jsonArray = new JSONArray(responseData);
                    Arrays.fill(bb_month, 0);
                    Arrays.fill(ab1_month, 0);
                    Arrays.fill(ab2_month, 0);
                    Arrays.fill(bl_month, 0);
                    Arrays.fill(al1_month, 0);
                    Arrays.fill(al2_month, 0);
                    Arrays.fill(bd_month, 0);
                    Arrays.fill(ad1_month, 0);
                    Arrays.fill(ad2_month, 0);
                    Arrays.fill(bs_month, 0);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 1) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    bb_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 2) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    ab1_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 3) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    ab2_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 4) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    bl_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 5) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    al1_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 6) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    al2_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 7) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    bd_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 8) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    ad1_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 9) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    ad2_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                        if (jsonArray.getJSONObject(i).getInt("time_slot") == 10) {
                            for (int j = 0; j < 30; j++) {
                                if (DateFormatUtils.long2Str(DateFormatUtils.str2Long(jsonArray.getJSONObject(i).getString("time"), false), false).compareTo(time_month.get(j)) == 0)
                                    bs_month[j] = jsonArray.getJSONObject(i).getDouble("bg_value");
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainBg.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
        System.out.println("day_bg: ");
        System.out.println(day_bg[0]);
    }


    public void someday(View v) {
        View view = LayoutInflater.from(MainBg.this).inflate(R.layout.bg_input_layout, null);
        final EditText year = (EditText) view.findViewById(R.id.year);
        final EditText month = (EditText) view.findViewById(R.id.month);
        final EditText day = (EditText) view.findViewById(R.id.day);

        new AlertDialog.Builder(this)
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (TextUtils.isEmpty(year.getText())
                                || TextUtils.isEmpty(month.getText())
                                || TextUtils.isEmpty(day.getText())) {
                            Toast.makeText(MainBg.this, "请完善日期！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        boolean result = calendarView.toSpecifyDate(Integer.valueOf(year.getText().toString()),
                                Integer.valueOf(month.getText().toString()),
                                Integer.valueOf(day.getText().toString()));
                        if (!result) {
                            Toast.makeText(MainBg.this, "日期越界！", Toast.LENGTH_SHORT).show();
                        } else {
                            chooseDate.setText("当前选中的日期：" + year.getText() + "年" + month.getText() + "月" + day.getText() + "日");
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    public void today(View view) {
        calendarView.today();
        chooseDate.setText("当前选中的日期：" + cDate[0] + "年" + cDate[1] + "月" + cDate[2] + "日");
    }

    public void lastMonth(View view) {
        calendarView.lastMonth();
    }

    public void nextMonth(View view) {
        calendarView.nextMonth();
    }

    public void start(View view) {
        calendarView.toStart();
    }

    public void end(View view) {
        calendarView.toEnd();
    }

    public void lastYear(View view) {
        calendarView.lastYear();
    }

    public void nextYear(View view) {
        calendarView.nextYear();
    }

}
